package com.segersten.kalaha.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.google.appengine.api.memcache.MemcacheService;

public class Event implements Serializable {
	
	private static final Event CREATED_EVENT = new Event(0, EventType.GAME_CREATED, null, null);
	
	private final int round;
	private final EventType type;
	private final Serializable arg;
	private final Event previuosEvent;
	private final Date created;
	

	private Event(int round, EventType type, Event previuosEvent, Serializable arg) {
		this.type = type;
		this.arg = arg;
		this.round = round;
		this.previuosEvent = previuosEvent;
		this.created = new Date();
	}
	
	public static List<Event> createEventList() {
		List<Event> res = new LinkedList<Event>();
		return res;
	}
	
	public static Event createEvent(Event previous, EventType type, Serializable arg) {
		return new Event(calcRound(previous), type, previous, arg);
	}

	private static int calcRound(Event previous) {
		return previous==null?1:previous.getRound()+1;
	}

	public EventType getType() {
		return type;
	}

	public Object getArg() {
		return arg;
	}

	public int getRound() {
		return round;
	}
	
	public Event getPreviuosEvent() {
		return previuosEvent;
	}

	public Date getCreated() {
		return created;
	}

	@Override
	public String toString() {
		return "Event [round=" + round + ", type=" + type + ", arg=" + arg
				+ ", previuosEventType=" + (previuosEvent==null?"null":previuosEvent.getType()) + ", created=" + created
				+ "]";
	}
	
}
